import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/app/app_constant.dart';
import 'package:tin_flutter/app/app_data.dart';
import 'package:tin_flutter/ui/activity/login/login_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/9/23
/// * 时间: 11:14
/// * 描述: 登录页面
class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final logic = Get.find<LoginLogic>();

  final _usernameController = TextEditingController(text: AppData.getUsername());
  final _passwordController = TextEditingController(text: AppData.getPassword());
  final _verifyCodeController = TextEditingController();

  /// 是否显示密码
  bool _isPasswordVisible = false;

  /// 验证码图片URL
  String _captchaUrl = AppConstant.base_url + 'api/getCode';

  @override
  void dispose() {
    Get.delete<LoginLogic>();
    super.dispose();
  }

  // 重新加载验证码
  void _reloadCaptcha() {
    setState(() {
      _captchaUrl =
          AppConstant.base_url + 'api/getCode?' + DateTime.now().millisecondsSinceEpoch.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Colors.blue[50]!,
              Colors.white,
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
          image: DecorationImage(
            image: AssetImage('assets/images/ic_bg_login.png'), // 假设背景图存在
            fit: BoxFit.cover,
          ),
        ),
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Logo
            Container(
              width: 107,
              height: 107,
              margin: EdgeInsets.only(top: 50, bottom: 20),
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/ic_logo.png'), // 假设logo存在
                  fit: BoxFit.contain,
                ),
              ),
            ),

            // 系统名称
            Text(
              '宏远运通停车系统',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),

            // 系统标语
            Container(
              margin: EdgeInsets.only(top: 8),
              child: Text(
                '智能、高效、快捷',
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                ),
              ),
            ),

            SizedBox(height: 12),

            // 用户名输入框
            _buildInputField(controller: _usernameController, hintText: '请输入用户名'),

            SizedBox(height: 20),

            // 密码输入框
            _buildInputField(
              controller: _passwordController,
              hintText: '请输入密码',
              obscureText: !_isPasswordVisible,
              suffixWidget: IconButton(
                icon: Icon(
                  _isPasswordVisible ? Icons.visibility_off : Icons.visibility,
                  color: Colors.blueAccent,
                ),
                onPressed: () {
                  setState(() {
                    _isPasswordVisible = !_isPasswordVisible;
                  });
                },
              ),
            ),

            SizedBox(height: 20),

            // 验证码输入框
            _buildInputField(
              controller: _verifyCodeController,
              hintText: '请输入验证码',
              suffixWidget: Container(
                child: GestureDetector(
                  onTap: _reloadCaptcha,
                  child: Image.network(
                    _captchaUrl,
                    width: 60,
                    height: 30,
                    fit: BoxFit.contain,
                  ),
                ),
              ),
            ),

            SizedBox(height: 40),

            // 登录按钮
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(left: 20, right: 20),
              height: 50,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [Colors.blue, Colors.blueAccent],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                borderRadius: BorderRadius.circular(20),
                boxShadow: [
                  BoxShadow(
                    color: Colors.blue.withOpacity(0.3),
                    blurRadius: 4,
                    spreadRadius: 2,
                  ),
                ],
              ),
              child: ElevatedButton(
                onPressed: () {
                  // 登录逻辑
                  logic.login(_usernameController.text, _passwordController.text,
                      _verifyCodeController.text);
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.transparent,
                  elevation: 0,
                ),
                child: Text(
                  '登录',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 17,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建输入框
  /// * [controller] 控制器
  /// * [hintText] 提示文本
  /// * [obscureText] 是否隐藏输入
  /// * [suffixWidget] 后缀控件
  Widget _buildInputField({
    required TextEditingController controller,
    required String hintText,
    bool obscureText = false,
    Widget? suffixWidget,
  }) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(left: 20, right: 20),
      padding: EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              controller: controller,
              obscureText: obscureText,
              decoration: InputDecoration(
                hintText: hintText,
                hintStyle: TextStyle(
                  color: Colors.grey,
                ),
                border: InputBorder.none,
                focusedBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
              ),
            ),
          ),
          if (suffixWidget != null) suffixWidget,
        ],
      ),
    );
  }
}
